﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HokjesSluiten
{
    public class StreepCollection
    {
        protected int xWidth;
        protected int yWidth;

        protected List<Streep> streepCollection = new List<Streep>();

        public StreepCollection(int x, int y)
        {
            xWidth = x;
            yWidth = y;
        }

        public Boolean addStreep(Streep streep)
        {
            if (doesStreepExist(streep))
                return false;

            streepCollection.Add(streep);
            return true;
        }

        public List<Streep> getMovesBy(Bot bot)
        {
            List<Streep> moves = new List<Streep>();
            foreach (Streep s in streepCollection)
            {
                if (s.moveMadeBy.botName == bot.botName && !s.isHokje)
                    moves.Add(s);
            }

            return moves;
        }

        public List<Streep> getHokjesBy(Bot bot)
        {
            List<Streep> moves = new List<Streep>();
            foreach (Streep s in streepCollection)
            {
                if (s.moveMadeBy.botName == bot.botName && s.isHokje)
                    moves.Add(s);
            }

            return moves;
        }

        public int countStrepen()
        {
            int strepen = 0;
            foreach (Streep streep in streepCollection)
            {
                if (streep.a.X == streep.b.X || streep.a.Y == streep.b.Y)
                    strepen++;
            }

            return strepen;
        }

        public int countHokjes()
        {
            int hokjes = 0;
            foreach (Streep streep in streepCollection)
            {
                if (streep.a.X != streep.b.X && streep.a.Y != streep.b.Y)
                    hokjes++;
            }

            return hokjes;
        }

        protected Boolean doesStreepExist(Streep streep)
        {
            foreach (Streep s in streepCollection)
            {
                if (streep.Equals(s))
                    return true;
            }

            return false;
        }

        #region Validate Hokjes
        
        public Streep hokjeLeft(Streep streep)
        {
            Boolean a = leftAbove(streep);
            Boolean b = leftBelow(streep);
            Boolean c = left(streep);

            if (a && b && c)
            {
                Streep hok = new Streep(
                    new Point(streep.a.X - 1, streep.b.Y),
                    streep.b);
                addStreep(hok);

                return hok;
            }

            return null;
        }

        public Streep hokjeRight(Streep streep)
        {
            Boolean a = rightAbove(streep);
            Boolean b = rightBelow(streep);
            Boolean c = right(streep);

            if (a && b && c)
            {
                Streep hok = new Streep(
                    streep.a,
                    new Point(streep.b.X + 1, streep.b.Y));
                addStreep(hok);

                return hok;
            }

            return null;
        }

        public Streep hokjeAbove(Streep streep)
        {
            Boolean a = aboveLeft(streep);
            Boolean b = aboveRight(streep);
            Boolean c = above(streep);

            if (a && b && c)
            {
                Streep hok = new Streep(
                    new Point(streep.a.X, streep.b.Y - 1),
                    streep.b);
                addStreep(hok);

                return hok;
            }

            return null;
        }

        public Streep hokjeBelow(Streep streep)
        {
            Boolean a = belowLeft(streep);
            Boolean b = belowRight(streep);
            Boolean c = below(streep);

            if (a && b && c)
            {
                Streep hok = new Streep(
                   streep.a,
                   new Point(streep.b.X, streep.b.Y + 1));
                addStreep(hok);

                return hok;
            }

            return null;
        }

        protected Boolean left(Streep streep)
        {
            Streep s2 = new Streep(
                new Point(streep.a.X - 1, streep.a.Y),
                new Point(streep.b.X - 1, streep.b.Y));

            return doesStreepExist(s2);
        }

        protected Boolean right(Streep streep)
        {
            Streep s2 = new Streep(
                new Point(streep.a.X + 1, streep.a.Y),
                new Point(streep.b.X + 1, streep.b.Y));

            return doesStreepExist(s2);
        }

        protected Boolean leftAbove(Streep streep)
        {
            Streep s2 = new Streep(
                new Point(streep.a.X, streep.a.Y),
                new Point(streep.b.X - 1, streep.b.Y - 1));

            return doesStreepExist(s2);
        }

        protected Boolean leftBelow(Streep streep)
        {
            Streep s2 = new Streep(
                new Point(streep.a.X + 1, streep.a.Y - 1),
                new Point(streep.b.X, streep.b.Y));

            return doesStreepExist(s2);
        }

        protected Boolean rightAbove(Streep streep)
        {
            Streep s2 = new Streep(
                new Point(streep.a.X, streep.a.Y),
                new Point(streep.b.X + 1, streep.b.Y - 1));

            return doesStreepExist(s2);
        }

        protected Boolean rightBelow(Streep streep)
        {
            Streep s2 = new Streep(
                new Point(streep.a.X + 1, streep.a.Y + 1),
                new Point(streep.b.X, streep.b.Y));

            return doesStreepExist(s2);
        }

        protected Boolean above(Streep streep)
        {
            Streep sBelow = new Streep(
                new Point(streep.a.X, streep.a.Y - 1),
                new Point(streep.b.X, streep.b.Y - 1));

            return doesStreepExist(sBelow);
        }

        protected Boolean below(Streep streep)
        {
            Streep sBelow = new Streep(
                new Point(streep.a.X, streep.a.Y + 1),
                new Point(streep.b.X, streep.b.Y + 1));

            return doesStreepExist(sBelow);
        }

        protected Boolean aboveLeft(Streep streep)
        {
            Streep sLeftBelow = new Streep(
                new Point(streep.a.X, streep.a.Y),
                new Point(streep.b.X - 1, streep.b.Y - 1));

            return doesStreepExist(sLeftBelow);
        }

        protected Boolean aboveRight(Streep streep)
        {
            Streep sRightBelow = new Streep(
                new Point(streep.a.X - 1, streep.a.Y + 1),
                new Point(streep.b.X, streep.b.Y));

            return doesStreepExist(sRightBelow);
        }

        protected Boolean belowLeft(Streep streep)
        {
            Streep sLeftBelow = new Streep(
                new Point(streep.a.X, streep.a.Y), 
                new Point(streep.b.X - 1, streep.b.Y + 1));

            return doesStreepExist(sLeftBelow);
        }

        protected Boolean belowRight(Streep streep)
        {
            Streep sRightBelow = new Streep(
                new Point(streep.a.X + 1, streep.a.Y + 1),
                new Point(streep.b.X, streep.b.Y));

            return doesStreepExist(sRightBelow);
        }

        #endregion

    }
}
